<?php

/**
 * 自定义表单
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: DiyFormModel.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace App\Model;
class DiyFormModel extends CommonModel{
    public $diyid;
    public $field_infos;
    public $name;
    public $table;
    //var $public;
    public function diyform($diyid) {
        $this->diyid = $diyid;
        $diyinfo = $this->getById($diyid);
        if(!isArrayAndNotEmpty($diyinfo)){
            die('参数不正确，该自定义表单不存在');
        }
        $this->field_infos = unserialize($diyinfo['fields']);
        $this->name = $diyinfo['formname'];
        $this->table = $diyinfo['formtable'];
        $this->public = $diyinfo['public'];
    }
    
    /**
     * 获取表单
     * @param type $formname
     * @return string
     */
    function getForm($formname = 'diy'){
        $fields = $this->field_infos;
        $formstring = '';
        $formfields = '';
        if(is_array($fields)){
            foreach($fields as $field){
                $formstring .= GetFormItem($field, $formname, 'diy');
                $formfields .= $formfields == '' ? $field['name'].','.$field['type'] : ';'.$field['name'].','.$field['type'];
            }
        }
        $formstring .= "<input type=\"hidden\" name=\"yd_fields\" value=\"".$formfields."\" />\n";
        $formstring .= "<input type=\"hidden\" name=\"yd_fieldshash\" value=\"".md5($formfields . C("COOKIE_ENCODE"))."\" />";
        return $formstring;
    }
    
    /**
     * 获取自定义表单数据
     * @param type $where
     * @return type
     */
    public function getAll($where) {
        $M = M($this->table);
        $count  = $M->where($where)->count();// 查询满足要求的总记录数
        //echo $M->getLastSql();
        $Page   = new \Think\Page($count, C("PAGE_SIZE"));// 实例化分页类 传入总记录数和每页显示的记录数
        $show   = $Page->show();// 分页显示输出
        $list   = $M->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $result = array('list' => $list, "page" => $show, "count" => $count);
        return $result;
    }
    
    /**
     * 获取单条数据
     * @param type $id
     * @return type
     */
    public function getDiyById($id) {
        $M = M($this->table);
        return $M->getById($id);
    }
}
